package com.shaoshaohuo.app.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/6/5.
 */

public class LoadListView extends ListView implements AbsListView.OnScrollListener {

    private boolean islast;
    private float starty;
    private float endy;

    private boolean isloading;
    private RelativeLayout linearLayout;
    private TextView textView;

    public LoadListView(Context context) {
        this(context, null);
    }

    public LoadListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化加载布局
        linearLayout = new RelativeLayout(context);
        linearLayout.setBackgroundColor(Color.WHITE);

        MyView myView = new MyView(context);
        int i = getResources().getDisplayMetrics().widthPixels / 14;
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(i, i);
        layoutParams1.setMargins(i * 2, 0, 0, 0);
        myView.setLayoutParams(layoutParams1);
        linearLayout.addView(myView);
        textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setText("正在加载");
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        textView.setLayoutParams(layoutParams);
        linearLayout.addView(textView);
//        addFooterView(linearLayout);
        linearLayout.setVisibility(GONE);
        //设置监听
        setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        if (islast && scrollState == SCROLL_STATE_IDLE && starty - endy > 0 && !isloading) {
            //可以加載
            loaddata();
        }
    }

    private void loaddata() {
        isloading = true;

        if(linearLayout.getParent()!=null){
            linearLayout.setVisibility(VISIBLE);
        }else {
            addFooterView(linearLayout);
            linearLayout.setVisibility(VISIBLE);
        }
        textView.setText("正在加载");
        if (loadListener != null) {
            loadListener.loadlistener();
        }
    }

    public void stopload(String value, boolean change) {
        if (change) {
            textView.setText(value);
        }
        removeFooterView(linearLayout);
        isloading = false;
        linearLayout.setVisibility(GONE);
        starty = 0;
        endy = 0;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        islast = firstVisibleItem + visibleItemCount == totalItemCount;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                starty = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                endy = ev.getY();

                break;
        }
        return super.onTouchEvent(ev);
    }

    private LoadListener loadListener;

    public void setLoadListener(LoadListener loadListener) {
        this.loadListener = loadListener;
    }

    public interface LoadListener {
        void loadlistener();
    }
}
